/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ProjetoView.java
 *
 * Created on 28/10/2011, 04:31:06
 */
package br.com.geftu.view;

import br.com.geftu.HibernateUtil.HibernateUtil;
import br.com.geftu.controle.ArrayListComboBoxModel;
import br.com.geftu.dao.DAO;
import br.com.geftu.model.Modulo;
import br.com.geftu.model.Projeto;
import br.com.geftu.util.AutoCompletion;
import br.com.geftu.util.MaskF;
import com.towel.el.annotation.AnnotationResolver;
import com.towel.swing.table.ObjectTableModel;
import java.awt.HeadlessException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import org.hibernate.Session;

/**
 *
 * @author jorgenei
 */
public final class ModuloView extends javax.swing.JFrame {

    List<Projeto> lista_projeto = new ArrayList();
    List<Modulo> lista_modulo = new ArrayList();
    Projeto projetosel = new Projeto();
    Integer posicao_atual = 0;
    String ordem = "order by idmod";
    String listar = "from Modulo ";

    /**
     * Creates new form ProjetoView
     */
    public ModuloView() {
        initComponents();
        popularComboboxProjeto();
        popularComboboxPesquisa();
        desativaBotoes();
//        preencherJtableModulos();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        groupordenar = new javax.swing.ButtonGroup();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jPanel1 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        rb_nome = new javax.swing.JRadioButton();
        rb_codigo = new javax.swing.JRadioButton();
        jButton4 = new javax.swing.JButton();
        cb_pesquisa = new javax.swing.JComboBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        tf_codigo = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        tf_altera = tf_altera = new JFormattedTextField(MaskF.mascara("data"));
        tf_modulo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        tf_release = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        cb_projeto = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        ckExclui = new javax.swing.JCheckBox();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.setName("jList1"); // NOI18N
        jScrollPane2.setViewportView(jList1);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Cadastro Módulos");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setName("jPanel1"); // NOI18N

        jLabel7.setText("Pesquisa:");
        jLabel7.setName("jLabel7"); // NOI18N

        jButton1.setText("Novo");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Salvar");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Excluir");
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jPanel4.setBackground(new java.awt.Color(204, 255, 153));
        jPanel4.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel4.setName("jPanel4"); // NOI18N

        jLabel8.setText("Ordenar:");
        jLabel8.setName("jLabel8"); // NOI18N

        rb_nome.setBackground(new java.awt.Color(204, 255, 153));
        groupordenar.add(rb_nome);
        rb_nome.setText("Nome");
        rb_nome.setName("rb_nome"); // NOI18N
        rb_nome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rb_nomeActionPerformed(evt);
            }
        });

        rb_codigo.setBackground(new java.awt.Color(204, 255, 153));
        groupordenar.add(rb_codigo);
        rb_codigo.setSelected(true);
        rb_codigo.setText("Código");
        rb_codigo.setName("rb_codigo"); // NOI18N
        rb_codigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rb_codigoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rb_nome)
                    .addComponent(rb_codigo))
                .addContainerGap(8, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rb_codigo)
                    .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rb_nome)
                .addContainerGap(11, Short.MAX_VALUE))
        );

        jButton4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jButton4.setText("Fechar");
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        cb_pesquisa.setName("cb_pesquisa"); // NOI18N
        cb_pesquisa.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cb_pesquisaItemStateChanged(evt);
            }
        });
        cb_pesquisa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_pesquisaActionPerformed(evt);
            }
        });
        cb_pesquisa.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                cb_pesquisaFocusLost(evt);
            }
        });
        cb_pesquisa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                cb_pesquisaKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cb_pesquisaKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3)
                        .addGap(63, 63, 63)
                        .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(29, 29, 29))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(cb_pesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(19, 19, 19))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(cb_pesquisa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButton1)
                            .addComponent(jButton2)
                            .addComponent(jButton3)
                            .addComponent(jButton4)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel2.setName("jPanel2"); // NOI18N
        jPanel2.setPreferredSize(new java.awt.Dimension(543, 200));

        jLabel1.setText("Código:");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel3.setText("Nome Projeto:");
        jLabel3.setName("jLabel3"); // NOI18N

        tf_codigo.setName("tf_codigo"); // NOI18N

        jLabel5.setText("Data Alteração:");
        jLabel5.setName("jLabel5"); // NOI18N

        tf_altera.setName("tf_altera"); // NOI18N

        tf_modulo.setName("tf_modulo"); // NOI18N
        tf_modulo.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tf_moduloFocusLost(evt);
            }
        });

        jLabel2.setText("Módulo:");
        jLabel2.setName("jLabel2"); // NOI18N

        tf_release.setName("tf_release"); // NOI18N

        jLabel4.setText("Release:");
        jLabel4.setName("jLabel4"); // NOI18N

        cb_projeto.setEditable(true);
        cb_projeto.setName("cb_projeto"); // NOI18N
        cb_projeto.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cb_projetoItemStateChanged(evt);
            }
        });
        cb_projeto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cb_projetoActionPerformed(evt);
            }
        });
        cb_projeto.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                cb_projetoFocusLost(evt);
            }
        });
        cb_projeto.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                cb_projetoInputMethodTextChanged(evt);
            }
        });
        cb_projeto.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                cb_projetoPropertyChange(evt);
            }
        });
        cb_projeto.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                cb_projetoKeyReleased(evt);
            }
        });
        cb_projeto.addVetoableChangeListener(new java.beans.VetoableChangeListener() {
            public void vetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {
                cb_projetoVetoableChange(evt);
            }
        });

        jLabel9.setText("Excluído do Projeto:");
        jLabel9.setName("jLabel9"); // NOI18N

        ckExclui.setName("ckExclui"); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(427, 427, 427))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(41, 41, 41)
                        .addComponent(tf_codigo, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cb_projeto, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tf_altera, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(32, 32, 32))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(tf_modulo, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(5, 5, 5)))
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(tf_release, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel2Layout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addComponent(jLabel9)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ckExclui)))))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(tf_codigo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3)
                        .addComponent(cb_projeto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tf_modulo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(tf_release, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(19, 19, 19)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(tf_altera, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(ckExclui))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        AutoCompletion.enable(cb_projeto);

        jPanel3.setName("jPanel3"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N
        jScrollPane1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jScrollPane1KeyPressed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 510, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(76, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 501, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(235, 235, 235))
        );

        setSize(new java.awt.Dimension(526, 486));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try {
            Modulo modulo = new Modulo();

            if (tf_codigo.getText().equals("")) {
                modulo.setProjeto((Projeto) cb_projeto.getSelectedItem());
                modulo.setModulo(tf_modulo.getText().toString());
                modulo.setRelease(tf_release.getText().toString());
                modulo.setDtalteracao(tf_altera.getText().toString());
                DAO objmodulo = new DAO(modulo);
                objmodulo.insere();
            } else {
                String alterar = "Tem certeza de que deseja alterar o registro?";
                int opcao_escolhida = JOptionPane.showConfirmDialog(null, alterar, "Exclusão ", JOptionPane.YES_OPTION);
                if (opcao_escolhida == JOptionPane.YES_OPTION) {
                    modulo.setProjeto((Projeto) cb_projeto.getSelectedItem());
                    modulo.setIdmod(Integer.parseInt(tf_codigo.getText()));
                    modulo.setModulo(tf_modulo.getText().toString());
                    modulo.setRelease(tf_release.getText().toString());
                    modulo.setDtalteracao(tf_altera.getText().toString());

                    JOptionPane.showMessageDialog(null, modulo);
                    DAO objmodulo = new DAO(modulo);
                    objmodulo.insere();

                }
                preencherJtableModulos();

            }

            //preencheCombo();
        } catch (HeadlessException | NumberFormatException erro) {
            JOptionPane.showMessageDialog(null, "Erro na insersão : " + erro);
        }// TODO add your handling code here:        // TODO add your handling code here:
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (cb_pesquisa.getSelectedItem().equals(cb_projeto.getSelectedItem())) {
            cb_projeto.setEnabled(false);
            tf_modulo.requestFocus();
            JOptionPane.showMessageDialog(null, "teste1");
        } else {
            JOptionPane.showMessageDialog(null, "teste2");
        }
        AtivaCampos();
        tf_codigo.setText("");
        tf_modulo.setText("");
        tf_release.setText("");
        tf_altera.setText("");
        //cb_projeto.setSelectedItem(cb_pesquisa.getSelectedItem());
    }//GEN-LAST:event_jButton1ActionPerformed

    private void rb_nomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rb_nomeActionPerformed
        ordem = "order by modulo";
        preencherJtableModulos();

    }//GEN-LAST:event_rb_nomeActionPerformed

    private void rb_codigoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rb_codigoActionPerformed
        ordem = "order by idmod";
        preencherJtableModulos();
    }//GEN-LAST:event_rb_codigoActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        String excluir = "Tem certeza de que deseja excluir o Modulo " + tf_modulo.getText() + "?";
        int opcao_escolhida = JOptionPane.showConfirmDialog(null, excluir, "Exclusão ", JOptionPane.YES_OPTION);
        if (opcao_escolhida == JOptionPane.YES_OPTION) {
            try {
                Modulo modulo = new Modulo();
                modulo.setProjeto(projetosel);
                modulo.setIdmod(Integer.parseInt(tf_codigo.getText()));
                JOptionPane.showMessageDialog(null, tf_codigo.getText());
                DAO objmodulo = new DAO(modulo);
                objmodulo.excluir();
                jTable1.valueChanged(null);

            } catch (NumberFormatException | HeadlessException erro) {
                JOptionPane.showMessageDialog(null, "Erro na insersão : " + erro);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Registro não excluído.");
        }
    }
        // TODO add your handling code here:        // TODO add your handling code here:}//GEN-LAST:event_jButton3ActionPerformed

        private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
            encerrar();
            // TODO add your handling code here:
    }//GEN-LAST:event_jButton4ActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        encerrar();        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosed

    private void cb_projetoVetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {//GEN-FIRST:event_cb_projetoVetoableChange

    }//GEN-LAST:event_cb_projetoVetoableChange

    private void cb_projetoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cb_projetoKeyReleased
        if (tf_codigo.getText().trim().equals("")) {
            Projeto projeto = new Projeto();
            projeto = (Projeto) cb_projeto.getSelectedItem();
            JOptionPane.showMessageDialog(null, projeto.getDtfinal().toString());
        }        // TODO add your handling code here:
    }//GEN-LAST:event_cb_projetoKeyReleased

    private void cb_projetoPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_cb_projetoPropertyChange
        // TODO add your handling code here:
    }//GEN-LAST:event_cb_projetoPropertyChange

    private void cb_projetoInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {//GEN-FIRST:event_cb_projetoInputMethodTextChanged

    }//GEN-LAST:event_cb_projetoInputMethodTextChanged

    private void cb_projetoFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cb_projetoFocusLost

    }//GEN-LAST:event_cb_projetoFocusLost

    private void cb_projetoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_projetoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cb_projetoActionPerformed

    private void cb_projetoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cb_projetoItemStateChanged

    }//GEN-LAST:event_cb_projetoItemStateChanged

    private void cb_pesquisaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cb_pesquisaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cb_pesquisaActionPerformed

    private void cb_pesquisaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cb_pesquisaKeyReleased
        JOptionPane.showMessageDialog(rootPane, "teste");
        projetosel = (Projeto) cb_pesquisa.getSelectedItem();
        listar = "from Modulo where idproj like '%" + projetosel.getIdproj() + "%'";
        preencherJtableModulos();

        JOptionPane.showMessageDialog(rootPane, "teste");
    }//GEN-LAST:event_cb_pesquisaKeyReleased

    private void cb_pesquisaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cb_pesquisaKeyPressed
        JOptionPane.showMessageDialog(rootPane, "teste");
        projetosel = (Projeto) cb_pesquisa.getSelectedItem();
        listar = "from Modulo where idproj like '%" + projetosel.getIdproj() + "%'";
        preencherJtableModulos();
        JOptionPane.showMessageDialog(rootPane, "teste");
        // TODO add your handling code here:
    }//GEN-LAST:event_cb_pesquisaKeyPressed

    private void cb_pesquisaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_cb_pesquisaFocusLost
        // TODO add your handling code here:
    }//GEN-LAST:event_cb_pesquisaFocusLost

    private void cb_pesquisaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cb_pesquisaItemStateChanged
        projetosel = (Projeto) cb_pesquisa.getSelectedItem();
        listar = "from Modulo as modulo where (modulo.projeto.idproj) = '" + projetosel.getIdproj() + "'";
        preencherJtableModulos();        // TODO add your handling code here:
    }//GEN-LAST:event_cb_pesquisaItemStateChanged

    private void jScrollPane1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jScrollPane1KeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_jScrollPane1KeyPressed

    private void tf_moduloFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tf_moduloFocusLost
        jButton2.setEnabled(true);
// TODO add your handling code here:
    }//GEN-LAST:event_tf_moduloFocusLost

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        int linha = jTable1.getSelectedRow();
        cb_projeto.setSelectedItem(projetosel);
        tf_codigo.setText("" + jTable1.getValueAt(linha, 0));
        tf_modulo.setText("" + jTable1.getValueAt(linha, 1));
        tf_release.setText("" + jTable1.getValueAt(linha, 2));
        tf_altera.setText("" + jTable1.getValueAt(linha, 3));
// TODO add your handling code here:
    }//GEN-LAST:event_jTable1MouseClicked

    /**
     * @param args the command line arguments
     */
    //xx1
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ModuloView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ModuloView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ModuloView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ModuloView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ModuloView().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cb_pesquisa;
    private javax.swing.JComboBox cb_projeto;
    private javax.swing.JCheckBox ckExclui;
    private javax.swing.ButtonGroup groupordenar;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JRadioButton rb_codigo;
    private javax.swing.JRadioButton rb_nome;
    private javax.swing.JFormattedTextField tf_altera;
    private javax.swing.JTextField tf_codigo;
    private javax.swing.JTextField tf_modulo;
    private javax.swing.JTextField tf_release;
    // End of variables declaration//GEN-END:variables

    public void popularComboboxProjeto() {
        Session sessao = HibernateUtil.getSessionFactory().openSession();
        lista_projeto = sessao.createQuery("from Projeto order by nomeprojeto").list();

        ArrayListComboBoxModel modelolista = new ArrayListComboBoxModel(lista_projeto);
        modelolista = new ArrayListComboBoxModel(lista_projeto);
        cb_projeto.setModel(modelolista);
        projetosel = (Projeto) cb_projeto.getSelectedItem();
        //  cb_pesquisa.setSelectedItem(cb_projeto.getSelectedItem());
    }

    public void popularComboboxPesquisa() {
        Session sessao = HibernateUtil.getSessionFactory().openSession();
        lista_projeto = sessao.createQuery("from Projeto order by nomeprojeto").list();

        ArrayListComboBoxModel modelolista = new ArrayListComboBoxModel(lista_projeto);
        modelolista = new ArrayListComboBoxModel(lista_projeto);
        cb_pesquisa.setModel(modelolista);
    }

    public void desativaBotoes() {
        jButton1.setEnabled(false);
        jButton2.setEnabled(false);
        jButton3.setEnabled(false);
    }

    public void AtivaBotoes() {
        jButton1.setEnabled(true);
        jButton2.setEnabled(true);
        jButton3.setEnabled(true);
    }

    public void DesativaCampos() {
        cb_projeto.setEnabled(false);
        tf_modulo.setEnabled(false);
        tf_release.setEnabled(false);
        tf_altera.setEnabled(false);
        jButton2.setEnabled(false);
        jButton3.setEnabled(false);

    }

    public void AtivaCampos() {
        cb_projeto.setEnabled(true);
        tf_modulo.setEnabled(true);
        tf_release.setEnabled(true);
        tf_altera.setEnabled(true);
        AtivaBotoes();

    }

    public void preencherJtableModulos() {
        ObjectTableModel<Modulo> model = new ObjectTableModel<Modulo>(
                new AnnotationResolver(Modulo.class), "idmod,modulo,release,dtalteracao");
        model.clear();

        model.setEditableDefault(true);

        AtivaBotoes();

        try {
            Session sessao = HibernateUtil.getSessionFactory().openSession();

            lista_modulo = sessao.createQuery(listar + ordem).list();

            for (int i = 0; i < lista_modulo.size(); i++) {
                Modulo modulo = lista_modulo.get(i);
                model.add(modulo);
            }

            if (model.getRowCount()>0) {
             System.out.println(lista_modulo);
             Modulo modulo = lista_modulo.get(0);
             cb_projeto.setSelectedItem(modulo.getProjeto());
             tf_codigo.setText("" + modulo.getIdmod());
             tf_modulo.setText(modulo.getModulo());
             tf_release.setText(modulo.getRelease());
             tf_altera.setText(modulo.getDtalteracao());
             //  tf_codigo.disable();
             AtivaCampos();
             } else {
             cb_projeto.setSelectedItem(cb_pesquisa.getSelectedItem());
             tf_codigo.setText("");
             tf_modulo.setText("");
             tf_release.setText("");
             tf_altera.setText("");
             DesativaCampos();
             }
        } catch (Exception erro) {
            JOptionPane.showMessageDialog(null, "Erro na insersão : " + erro);
        }
        //   JTable table = new JTable(model);
        jTable1.setModel(model);

        jScrollPane1.setViewportView(jTable1);
    }

    // Permite tratar eventos do mouse
    public void encerrar() {
        setVisible(false);
        RelatoView.getInstance().setVisible(true);
    }
    //   private static List<Modulo> linhasModulo;

}
